﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using crm_library.DataAccess.TableReps;
using crm_library.DataAccess.ObjectReps;
using crm_library.DataAccess;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace crm_dal.Objects
{
	[DataContractAttribute]
	public class Sale : ObjectRepresentation
	{
		#region constructor
		public new const string TableName = "Sale";
		public new const string ObjectName = "Sale";

		public Sale(int saleID) : base(TableName, ObjectName)
		{
			SaleID = saleID;
			Load();
		}

		public Sale()
			: base(TableName, ObjectName)
		{

		}
		#endregion constructor

		#region properties
		[ListingFieldAttribute("Sale ID")]
		[DataFieldAttribute("SaleID", false, true)]
		[DataMember]
		public int SaleID { get; set; }

		[DataFieldAttribute("ClientID", false)]
		[DataMember]
		public int ClientID { get; set; }

		private Client _clientObj;
		public Client ClientObj
		{
			get
			{
				if (_clientObj == null)
				{
					_clientObj = new Client(ClientID);
				}
				return _clientObj;
			}
			set
			{
				_clientObj = value;
				if (value != null)
				{
					ClientID = _clientObj.ClientID;
				}
				else
				{
					ClientID = -1;
				}
			}
		}

		[ListingFieldAttribute("Client Name")]
		public string ClientName
		{
			get { return ClientObj.LastCommaFirstName; }
		}



		[DataFieldAttribute("UserID", false)]
		[DataMember]
		public int UserID { get; set; }

		private User _userObj;
		public User UserObj
		{
			get
			{
				if (_userObj == null)
				{
					_userObj = new User(UserID);
				}
				return _userObj;
			}
		}

		[ListingFieldAttribute("User Name")]
		public string UserName
		{
			get { return UserObj.LoginName; }
		}

		[ListingFieldAttribute("Date")]
		[DataFieldAttribute("Date", false)]
		[DataMember]
		public DateTime Date { get; set; }

        [DataFieldAttribute("ProductID", false)]
		[DataMember]
        public int ProductID { get; set; }


		public decimal Total
		{
			get
			{
				var _total = 0.0M;
					foreach (var saleItem in SaleItems)
					{
						_total += saleItem.Total;
					}
				return _total;
			}
		}
		[ListingFieldAttribute("Total")]
		public string TotalStr
		{ 
			get
			{
				return Total.ToString(); //TODO: Format to currency.
			}
		}

		[DataMember]
		private List<SaleItem> _saleItems;
		public List<SaleItem> SaleItems
		{
			get
			{
				if (_saleItems == null)
				{
					_saleItems = new List<SaleItem>();
					_saleItems.AddRange(SaleItem.GetAll().Where(item => item.SaleID == SaleID).ToArray());
				}
				return _saleItems;
			}
			set
			{
				_saleItems = value;
			}
		}


		#endregion properties

		#region ObjectRepresentation implementations
		public override int GetPKValue()
		{
			return SaleID;
		}

		public override void SetPKValue(int pk)
		{
			SaleID = pk;
		}

		public override int GetNextPKValue()
		{
			int maxInt = 0;

			foreach (var item in DB.ReadObjectList(new Sale()))
			{
				maxInt = Math.Max(maxInt, item.GetPKValue());
			}
			return maxInt + 1;
		}

		public override void Load(int pk)
		{
			DB.ReadObject(this, pk);
			SaleItems.AddRange(SaleItem.GetAll().Where(item => item.SaleID == pk));
		}

		public override void Update()
		{
			DB.UpdateObject(this, GetPKValue());
		}

		public override void Delete(int pk)
		{
			DB.DeleteObject(this, pk);
		}

		public override void Create()
		{
			DB.CreateObject(this);
		}

		public override ListItem ToListItem()
		{
			const string format = "{0} on {1}";
			return new ListItem(String.Format(format, ClientName, Date), GetPKValue().ToString());
		}

		#endregion ObjectRepresentation implementations

		public static List<Sale> GetAll()
		{
			return DB.ReadObjectList(new Sale());
		}
	}
}
